class AuctingsController < ApplicationController
  # GET /auctings
  # GET /auctings.json
  def index
    @auctings = Aucting.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @auctings }
    end
  end

  # GET /auctings/1
  # GET /auctings/1.json
  def show
    @aucting = Aucting.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @aucting }
    end
  end

  # GET /auctings/new
  # GET /auctings/new.json
  def new
    @aucting = Aucting.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @aucting }
    end
  end

  # GET /auctings/1/edit
  def edit
    @aucting = Aucting.find(params[:id])
  end

  # POST /auctings
  # POST /auctings.json
  def create
    @aucting = Aucting.new(params[:aucting])
    if(@aucting.product.auctings.last==nil)
      respond_to do |format|
        if @aucting.save
          @aucting.product.user_winner_id=@aucting.user.id
          product=@aucting.product;
          product.save
          format.html { redirect_to @aucting.product.auction, notice: 'Aucting was successfully created.' }
          format.json { render json: @aucting, status: :created, location: @aucting }
        else
          format.html { render action: "new" }
          format.json { render json: @aucting.errors, status: :unprocessable_entity }
        end
      end
    else
      if((@aucting.price>@aucting.product.auctings.last.price)&&(@aucting.product.auction.auction_type_id==1))||(@aucting.product.auction.auction_type_id==2)
        respond_to do |format|
          if @aucting.save
            @aucting.product.user_winner_id=@aucting.user.id
            product=@aucting.product;
            if(@aucting.price>=product.max_price)&&(@aucting.product.auction.auction_type_id!=2)
              product.sold_out=1
            end
            product.save
            format.html { redirect_to @aucting.product.auction, notice: 'Aucting was successfully created.' }
            format.json { render json: @aucting, status: :created, location: @aucting }
          else
            format.html { render action: "new" }
            format.json { render json: @aucting.errors, status: :unprocessable_entity }
          end
        end
      else
        respond_to do |format|
          flash[:notice] = "Your price is not accepted"
          format.html { redirect_to @aucting.product.auction }
          #format.json { render json: @aucting.errors, status: :unprocessable_entity }
        end
      end

    end

  end

  # PUT /auctings/1
  # PUT /auctings/1.json
  def update
    @aucting = Aucting.find(params[:id])

    respond_to do |format|
      if @aucting.update_attributes(params[:aucting])
        format.html { redirect_to @aucting, notice: 'Aucting was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @aucting.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /auctings/1
  # DELETE /auctings/1.json
  def destroy
    @aucting = Aucting.find(params[:id])
    @aucting.destroy

    respond_to do |format|
      format.html { redirect_to auctings_url }
      format.json { head :no_content }
    end
  end
  def aucting_of_product
    @auction= Product.find(params[:id])

    render :partial => "auctions/list_aucting", :locals => {:product=>@auction}

  end
end
